﻿

#region Using Statements

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Threading;
using System.ComponentModel;
using System.Reflection;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Design;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using Freeform;
using FGame = Freeform.Game;
using XGame = Microsoft.Xna.Framework.Game;
using FComponent = Freeform.Component;
using FViewport = Freeform.Viewport;
#endregion


namespace Freeform
{
    /// <summary>
    /// A class that renders a 3D world to a location on the screen.
    /// </summary>
    public class Viewport3D : FViewport, IGeometric2D
    {

        #region Events

        #endregion


        #region Members

        /// <summary>
        /// Gets the camera for the viewport.
        /// </summary>
        public Camera3D Camera { get; set; }

        #endregion


        #region Init

        public Viewport3D(World World)
            : base(World)
        {
            new Camera3D(this);
        }

        #endregion


        #region Update

        #endregion


        #region Draw

        public override void Draw(GameTime Time)
        {
            base.Draw(Time);

            // Set graphics device settings appropriate for 3d rendering.
            DepthStencilState DepthState = new DepthStencilState();
            DepthState.DepthBufferEnable = true;
            DepthState.DepthBufferWriteEnable = true;
            Game.GraphicsDevice.BlendState = BlendState.Opaque;
            Game.GraphicsDevice.DepthStencilState = DepthState;
            RasterizerState RastState = new RasterizerState();
            RastState.CullMode = CullMode.None;
            //RastState.FillMode = FillMode.WireFrame;
            Game.GraphicsDevice.RasterizerState = RastState;
            SamplerState ModelSamplerState = new SamplerState();
            ModelSamplerState = SamplerState.LinearWrap;
            Game.GraphicsDevice.SamplerStates[0] = ModelSamplerState;

            // Render each entity.
            for (int i = 0; i < World.ComponentList.Loaded.Count; i++)
            {
                Entity3D Entity = World.ComponentList.Loaded[i] as Entity3D;
                if (Entity != null && Entity.IsVisible && !Entity.IsDisposed)
                    Entity.Draw(Time, this);
            }

            // Start the spritebatch.
            SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);

            // Render debug text.
            if (DebugText != null && DebugFont != null)
                SpriteBatch.DrawString(DebugFont, DebugText, new Vector2(5, 5), DebugColour);

            // End scene.
            SpriteBatch.End();

        }

        #endregion


        #region Dispose


        #endregion


        #region Utility

        #endregion


        #region Implementation

        #endregion

    }
}
